﻿using DXBase;
using DXBase.D2D;
using DXBase.DWrite;
using DXBase.Scenes;
using DXBase.Structs;
using System;
using Windows.Foundation;
using Windows.UI;

namespace Sample2_D2D
{
	public class D2DScene : IScene
	{
		public InputController Inputcontroller { get; set; }

		SolidColorBrush bRed, bBlue, bTransBlue;
		BitmapBrush bImage;
		LinearGradientBrush bYRB;
		StrokeStyle sDash;

		Rect rEllipse = new Rect(0, 0, 400, 300);

		TextLayout lText;
		IGeometry gSkewedRect;
		PathGeometry gPath;

		public D2DScene()
		{
			bImage = new BitmapBrush { Bitmap = new Bitmap(), };
			bImage.Bitmap.CreateWic("Assets\\space-background.jpg");

			bYRB = new LinearGradientBrush
			{
				StartPoint = new Vector2F(100, 100),
				EndPoint = new Vector2F(800, 800),
				GradientStops = new [] 
				{
					new GradientStop { position = 0, color = (Color4F)Colors.Yellow },
					new GradientStop { position = 0.4f, color = (Color4F)Colors.Red },
					new GradientStop { position = 1, color = (Color4F)Colors.Blue},
				},
			};

			bRed = new SolidColorBrush() { Color = (Color4F)Colors.Red };
			bBlue = new SolidColorBrush() { Color = (Color4F)Colors.Blue };
			bTransBlue = new SolidColorBrush() { Color = (Color4F)new Color { A = 100, B = 255 } };
			sDash = new StrokeStyle()
			{
				DashStyle = DASH_STYLE.CUSTOM,
				DashCap = CAP_STYLE.ROUND,
				Dashes = new float[] { 10, 20, 30 },
			};

			lText = new TextLayout(
				"Hello DWrite!", 
				new TextFormat()
				{
					FontSize = 48,
					FontWeight = FONT_WEIGHT.BLACK,
				},
				200, 200);

			gSkewedRect = new TransformedGeometry(Matrix3x2F.Skew(0, -30), new RectangleGeometry(new Rect(0, 0, 200, 200)));
			
			gPath = new PathGeometry();
			using (var sink = gPath.Open())
			{
				sink.BeginFigure(new Vector2F(), FIGURE_BEGIN.FILLED);
				sink.AddLines(new float2[] {
					new Vector2F(25, 200),
					new Vector2F(275, 175),
					new Vector2F(50, 30),
				});
				sink.EndFigure(FIGURE_END.OPEN);
			}
		}

		Vector2F pText;
		float rText;
		Vector2F pSkewedRect, pEllipse, pPath;
		public void Update(DXContext ctxt, PerformanceTimer ptimer) 
		{
			var t = ptimer.Total;
			rText = 30 * (float)Math.Cos(t * 0.3);
			pText = Along(100, 50, t);
			pSkewedRect = Along(100, 100, t);
			pEllipse = Along(-25, 50, t);
			pPath = Along(50, -75, t);
		}
		Vector2F Along(float dx, float dy, float t)
		{
			dx *= t;
			dy *= t;
			dx %= 1000;
			dy %= 800;
			if (dx < 0)
				dx += 1000;
			if (dy < 0)
				dy += 800;
			return new Vector2F(dx, dy);
		}

		public void Render(DXContext ctxt)
		{
			var g = new D2DGraphics(ctxt);
			g.Clear((Color4F)Colors.Beige);

			g.Transform = Matrix3x2F.Translate(pSkewedRect);
			g.FillGeometry(gSkewedRect, bImage);
			g.DrawGeometry(gSkewedRect, bBlue, 12, sDash);

			g.Transform = Matrix3x2F.Translate(pEllipse);
			g.DrawEllipse(rEllipse, bYRB, 10, null);

			g.Transform = Matrix3x2F.Translate(pPath);
			g.FillGeometry(gPath, bTransBlue);
			g.DrawGeometry(gPath, bBlue);

			g.Transform = Matrix3x2F.Rotation(rText, pText);
			g.DrawTextLayout(pText, lText, bYRB);
		}
	}
}
